//
//  SinaPictureView.swift
//  weibo
//
//  Created by sirius on 16/1/7.
//  Copyright © 2016年 sirius. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


let picMargin:CGFloat = 5
let itemmargin:CGFloat = 5
let itemwidth = (UIScreen.mainScreen().bounds.width - CGFloat(SinaHomeMargin) * 2 - 2 * itemmargin ) / 3

//设置可重用的标示符
let SinaPictureViewReuseIdentifier = "SinaPictureReuseIdentfier"

class SinaPictureView: UICollectionView,UICollectionViewDataSource  {

    var pictures: [SinaPictureModel]?{
        didSet{
            //MARK:------1.计算picutreView的高度
            let size = getSize()
            //更新宽高约束
            snp_updateConstraints { (make) -> Void in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            textLabel.text = "\(pictures?.count ?? 0)"
             //MARK:-------1.赋值后得reloadData
            reloadData()
        }
    }
    init(){
        
    super.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        addSubview(textLabel)
        textLabel.snp_updateConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
        //MARK:----2.设置代理方法，设置布局
        //MARK:---实现数据源的方法
        dataSource = self
        
        //注册cell
        //自定义的cell
        registerClass(SinaPictureViewCollectionVewCell.self, forCellWithReuseIdentifier: SinaPictureViewReuseIdentifier)
        

        //设置 item
        //获取系统的布局 强制转换成流水布局
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = CGSizeMake(itemwidth, itemwidth)
        //设置横纵向间距
        flowLayout.minimumInteritemSpacing = itemmargin
        flowLayout.minimumLineSpacing = itemmargin
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func getSize() ->CGSize {
        /*
        计算配图的宽高
        高度是图片的高度
        
        
        
        一张图片给他一个固定的宽高
        
        4张图片的时候返回一个 2*2的吧
        
        2,3 一个图片的高度,宽度是一个屏幕的宽度
        5,6, 2*图片的高度+间距,宽度 一个屏幕的宽度
        7,8,9 3*图片的高度+ 2*间距,宽度 一个屏幕的宽度
        
        */
        let count = pictures?.count
        
        if count == 1 {
        
            return CGSizeMake(itemwidth, itemwidth)
        
        }else if count == 4 {
        
            return CGSizeMake(itemwidth * 2, itemwidth)
        
        }else{
            
            let row = (count! - 1 ) / 3 + 1
            
            let height = CGFloat(row) * (itemwidth) + ( CGFloat(row) - 1) * itemmargin
            
            
            return CGSizeMake((UIScreen.mainScreen().bounds.width) - CGFloat(SinaHomeMargin) * 2, height)
        }
        return CGSizeZero
    }
    lazy var textLabel: UILabel = UILabel()
}
extension SinaPictureView {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SinaPictureViewReuseIdentifier, forIndexPath: indexPath)as! SinaPictureViewCollectionVewCell
        //改变cell 的背景颜色
        cell.backgroundColor = UIColor.yellowColor()
        cell.photoModel = pictures![indexPath.item]
        
        return cell
    }

}                                       //UICollectionViewCell
class SinaPictureViewCollectionVewCell: UICollectionViewCell {
     //设置模型
    var photoModel: SinaPictureModel?
        {
        didSet{
            
            //图片赋值
            let url = NSURL(string: photoModel?.thumbnail_pic ?? "")
            
            iconImageView.sd_setImageWithURL(url)
//            self.backgroundColor = UIColor.yellowColor()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("你错~\\(≧▽≦)/\\~啦啦啦")
    }
    private func setupUI(){
        addSubview(iconImageView)
        iconImageView.frame = bounds
    }
    lazy var iconImageView: UIImageView = UIImageView()
}
